/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmAlumno;

import educar.models.Alumno;
import educar.models.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;

/**
 *
 * @author Matias
 */
public class AgregarAlumnoController implements ActionListener, WindowListener {
    
    private AgregarAlumnoView agregarAlumnoView;
    private String vector[] = new String[4];
    private AlumnoController alumnoControl;
    
// <editor-fold defaultstate="collapsed" desc="Constructor">
    public AgregarAlumnoController(AlumnoController alumnoController){        
        alumnoControl = alumnoController;
        agregarAlumnoView = new AgregarAlumnoView();
        agregarAlumnoView.setActionListener(this);
        agregarAlumnoView.setWindowListener(this);
    }
 // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        agregarAlumnoView.setVisible(true);
    }
// </editor-fold>  

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (agregarAlumnoView.getJbCancelar() == source){
            agregarAlumnoView.dispose();
        }
        if (agregarAlumnoView.getJbAceptar() == source){
            vector[0] = agregarAlumnoView.getNroAlumno(); 
            vector[1] = agregarAlumnoView.getDni();
            vector[2] = agregarAlumnoView.getNombreYApellido();
            vector[3] = agregarAlumnoView.getDireccion();
            if (!((vector[0].length() == 0) || (vector[1].length() == 0) || (vector[2].length() == 0) || (vector[3].length()== 0))) {
                Alumno alumno = new Alumno(vector);
                User user = new User(vector[2], vector[1], "Alumno");
                if (alumno.save() && user.save()) { 
                            alumnoControl.agregarATabla(vector);
                            agregarAlumnoView.dispose();
                } else {
                    JOptionPane.showMessageDialog(null,"Ya existe un registro con el numero de alumno "+ agregarAlumnoView.getNroAlumno()+".","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null,"Existe al menos un campo sin completar.\n\nRevise los campos ingresados","proyecto 2012",JOptionPane.WARNING_MESSAGE);
            }
        }
    }    
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed no usadas">
    @Override
    public void windowOpened(WindowEvent e) {
        
    }

    @Override
    public void windowClosing(WindowEvent e) {
        
    }

    @Override
    public void windowClosed(WindowEvent e) {
        
    }

    @Override
    public void windowIconified(WindowEvent e) {
        
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        
    }

    @Override
    public void windowActivated(WindowEvent e) {
        
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        
    }
// </editor-fold>  
}   
